package com.ncp.model.biz.bbs;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ncp.model.dao.DB;
import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.comment.Articlebean;

public class ArticleProcess {
	
	private Connection conn=null;
	private PreparedStatement ps=null;
	private ResultSet rs=null;
	private int pageCount=0;
	private int rowCount=0;
	private int pageSize=10;
	/**
	 * 发表主题
	 * @param clicks点击量
	 * @param title主题
	 * @param cont内容
	 * @param username用户名
	 * @return
	 */
	public boolean Postsubject(int clicks,String title,String cont,String username){
		boolean b=false;
		String sql="insert into barticle values (null,"+clicks+",'"+title+"', '"+cont+"','"+username+"', now())";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			int n=ps.executeUpdate();
			if(n!=0){
				b=true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
		return b;
	}
	
	/**
	 * 展示所有主题信息
	 * @return
	 */
	public ArrayList<Articlebean> getArticle(int nowPage){
		ArrayList<Articlebean> list=new ArrayList<Articlebean>();
		String sql="select * from barticle  limit "+pageSize*(nowPage-1)+","+pageSize+"";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Articlebean ab=new Articlebean();
				ab.setId(rs.getInt("id"));
				ab.setTitle(rs.getString("title"));
				ab.setUsername(rs.getString("username"));
				ab.setPdate(rs.getTimestamp("pdate"));
				ab.setClicks(rs.getInt("clicks"));
				list.add(ab);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
		return list;
	}
	
	/**
	 * 展示主题的具体信息
	 * @param id
	 * @return
	 */
	public Articlebean showArticleDetail(int id){
		Articlebean ab=new Articlebean();
		String sql="select * from barticle where id="+id;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				ab.setId(rs.getInt("id"));
				ab.setUsername(rs.getString("username"));
				ab.setPdate(rs.getTimestamp("pdate"));
				ab.setConn(rs.getString("conn"));
				ab.setTitle(rs.getString("title"));
			}
			return ab;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
		return ab;
	}
	
	/**
	 * 增加帖子浏览量
	 * @param id
	 */
	public void addClick(int id){
		int click=0;
		String sql="select clicks from barticle where id="+id;
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				click=rs.getInt("clicks");
			}
			click++;
			String sql2="update barticle set clicks="+click+" where id="+id;
			ps=conn.prepareStatement(sql2);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
	}
	
	/**
	 * 删除帖子
	 * @param id
	 * @return
	 */
	public boolean deleteArticle(int id){
		boolean b=false;
		Statement sm=null;
		String sql="delete from barticle where id="+id;
		String sql2="delete from reply where rootid="+id;
		try {
			conn=DbConnection.getConnByDataSource();		
			sm=conn.createStatement();
			sm.addBatch(sql);
			sm.addBatch(sql2);
			int a[]=sm.executeBatch();
			for(int i=0;i<a.length;i++){
			}
			b=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				if(sm!=null){
					sm.close();
				}
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return b;
	}
	
	/**
	 * 得到帖子的总数量
	 * @return
	 */
	public int getArticleNum(){
		int count=0;
		String sql="select count(*) from barticle";
		try {
			conn=DbConnection.getConnByDataSource();
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()){
				count=rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			new DB().closeSQL(conn, ps);
		}
		return count;
	}
	
	/**
	 * 得到帖子的总页数
	 * @return
	 */
	public int getArticclePageCount(){
		rowCount=this.getArticleNum();
		if(rowCount%pageSize==0){
			pageCount=rowCount/pageSize;
		}else if(rowCount%pageSize!=0){
			pageCount=rowCount/pageSize+1;
		}
		return pageCount;
	}

}
